<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ServiceAction
 *
 * @author ASIMO
 */
class ServiceAction {
    
    public function __construct() {
        set_time_limit(0);
    }
    
    //处理离职申请、辞退等延时流程
    protected function process() {
        //申请离职、辞退流程
        $now = time();
        $resJobuser = M('job_user')->where('state=4 or state=5')->select();
        if ($resJobuser) {
            foreach ($resJobuser as $user) {
                if ($now-$user['uptime']>C('APPLY_TIMEOUT')) {//超时
                    $ret = M('job_user')->where('id='.$user['id'])->delete();
                    if (!$ret) {
                        continue;
                    }
                    //记入任职历史
                    $resJob = M('job')->where("id=".$user['jobid'])->find();
                    $quitype = \App\Common\Constvars::JOBHIS_QUITYPE_UNSETTLE;
                    if ($resJob['moneyin']==\App\Common\Constvars::JOB_SALARYSTATUS_SETTLE) {
                        $quitype = \App\Common\Constvars::JOBHIS_QUITYPE_SETTLE;
                    }
                    $jobhistory = array(
                        'quitype' => $quitype,
                        'quitime' => time()
                    );
                    M('jobhistory')->where("uid=".$user['uid']." and jobid=".$user['jobid']." and quitime=0")->save($jobhistory);
                    //更新用户资料
                    $resUserinfo = M('user_info')->where('uid='.$user['uid'])->find();
                    $teaminfo = array(
                        'teamname'=>'',
                        'teamid'=>-1,
                        'projectname'=>'',
                        'projectid'=>-1,
                    );
                    M('user_info')->where('uid='.$user['uid'])->save($teaminfo);
                    //更新职位统计
                    M('job')->where('id='.$user['jobid'])->setDec('datastate');
                    M('team')->where('id='.$resUserinfo['teamid'])->setDec('jobsok');
                    
                    //发送通知
                    //通知本人
                    $content = "";
                    if ($user['state']==4) {
                        $content = "<td>您被辞退且超过申述期限，已自动退出团队<b>[".$resUserinfo['teamname']."]</b></td>";
                    }
                    elseif($user['state']==5){
                        $content = "<td>您申请离职且超过缓冲期限，已自动退出团队<b>[".$resUserinfo['teamname']."]</b></td>";
                    }
                    $content = logPost($content, "", $now);
                    sendNotice($user['uid'], $content);
                    //通知原团队其他人
                    $content = '';
                    if ($user['state']==4) {
                        $content = "用户<a href='".genTalentURL($user['uid'])."'><b>".$user['uname']."</b></a>已被辞退，离开团队。";
                    }
                    elseif ($user['state']==5){
                        $content = "用户<a href='".genTalentURL($user['uid'])."'><b>".$user['uname']."</b></a>提出离职申请已过期限，已自动退出团队。";
                    }
                    $content = logPost('<td>'.$content.'</td>', U('Team/members/@'.getDomain(), array('teamid'=>$resUserinfo['teamid'])), $now);
                    $log = array();
                    $log[] = array(
                        'uid'=>$resTeam['uid'],//团队创建者
                        'content'=>$content,
                        'ctime'=>$now,
                    );
                    $resPrj = M('project')->where('id='.$resUserinfo['projectid'].' and state>0')->find();
                    if ($resUserinfo['uid']!=$resPrj['uid']) {//项目创建者
                        $log[] = array(
                            'uid'=>$resPrj['uid'],//团队创建者
                            'content'=>$content,
                            'ctime'=>$now,
                        );
                    }
                    $other_fellow = M('job_user')->where('pid='.$resUserinfo['projectid'].' and state=2')->select();
                    foreach ($other_fellow as $fellow) {
                        if ($fellow['uid']!=$resPrj['uid']) {
                            $log[] = array(
                                'uid'=>$fellow['uid'],
                                'content'=>$content,
                                'ctime'=>$now,
                            );
                        }
                    }
                    $ret = M('office_log')->addAll($log);
                    //sendNoticeBatch($user['uid'], $resUserinfo['teamid'], $resUserinfo['projectid'], $content, true, true, true);
                }
            }
            echo 'success';
        }
        else{
            echo 'empty list';
        }
    }
    
    //发送通知
    public function sendlog() {
        $from = $_SERVER['SERVER_NAME'];
       // if ($from!='localhost') {
        //    exit;//只接受本地请求
        //}
        echo "Request from $from\n";
        
        $this->process();
        
        $content = "[sendlog]request from:".$from."\n";
        Log::write($content, Log::ERR);
    }
    
    //处理邮件队列
    public function processMailqueue() {
        $from = get_client_ip();
       // if ($from!='127.0.0.1') {
        echo "Request from $from\n";
            //exit;//只接受本地请求
        //}
            
        //是否正在执行
        $time = date('Y.m.d H:i:s',time());
        echo "[$time]Proccess Mailqueue\n";
        $tasklockTab = M('tasklock');
        $ret = $tasklockTab->where('tasktype=1')->find();
        if ($ret) {
            echo "[$time]上轮任务仍在执行......\n";
            Log::write('[processMailqueue]上轮任务仍在执行......\n', Log::ERR);
            exit;
        }
        
        $mqTab = M('mailqueue');
        $resMq = $mqTab->where('state=1')->select();
        if (1) {
            //开始执行，锁定
            $ret = $tasklockTab->add(array('tasktype'=>1,'status'=>1));
            if (!$ret) {
                Log::write('[processMailqueue]锁定失败!\n', Log::ERR);
                exit;
            }
            foreach ($resMq as $mail) {
                if (think_send_mail($mail['receiver'], $mail['name'], $mail['subject'], $mail['body'])) {
                    $mqTab->where('id='.$mail['id'])->setField('state', 0);
                }
                else{
                    Log::write("[处理邮件队列]发送失败:".$mail['id'].",".$mail['receiver']."\n", Log::ERR);
                }
            }
            $ret = $tasklockTab->where('tasktype=1')->delete();
            if (!$ret) {
                Log::write('[processMailqueue]解锁失败!\n', Log::ERR);
            }
            echo '[$time]success';
        }
        else{
            echo '[$time]mailqueue empty';
        }
    }
}
